<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
	<meta name="keywords" content="Dubbo,Nacos,registration center" />
	<meta name="description" content="Nacos with Dubbo fusion become registry" />
	<!-- 网页标签标题 -->
	<title>Nacos with Dubbo fusion become registry</title>
	<link rel="shortcut icon" href="https://img.alicdn.com/tfs/TB1hgJpHAPoK1RjSZKbXXX1IXXa-64-64.png"/>
	<link rel="stylesheet" href="/build/documentation.css" />
</head>
<body>
	<div id="root"><div class="documentation-page" data-reactroot=""><header class="header-container header-container-normal"><div class="header-body"><a href="/en-us/index.html"><img class="logo" src="/img/nacos_colorful.png"/></a><div class="search search-normal"><span class="icon-search"></span></div><span class="language-switch language-switch-normal">中</span><div class="header-menu"><img class="header-menu-toggle" src="/img/menu_gray.png"/><ul><li class="menu-item menu-item-normal"><a href="/en-us/index.html">HOME</a></li><li class="menu-item menu-item-normal menu-item-normal-active"><a href="/en-us/docs/quick-start.html">DOCS</a></li><li class="menu-item menu-item-normal"><a href="/en-us/blog">BLOG</a></li><li class="menu-item menu-item-normal"><a href="/en-us/community">COMMUNITY</a></li><li class="menu-item menu-item-normal"><a href="https://cn.aliyun.com/product/aliware/mse?spm=nacos-website.topbar.0.0.0">NACOS IN CLOUD</a><img class="menu-img" src="https://img.alicdn.com/tfs/TB1esl_m.T1gK0jSZFrXXcNCXXa-200-200.png"/></li><li class="menu-item menu-item-normal"><a href="http://console.nacos.io/nacos/index.html">DEMO-CONSOLE</a></li></ul></div></div></header><div class="bar"><div class="bar-body"><img src="/img/docs.png" class="front-img"/><div class="bar-title"><span>Documentation</span><div class="bone bone-light"></div></div><img src="/img/docs.png" class="back-img"/></div></div><section class="content-section"><div class="content-body"><div class="sidemenu"><div class="sidemenu-toggle"><img src="https://img.alicdn.com/tfs/TB1E6apXHGYBuNjy0FoXXciBFXa-200-200.png"/></div><ul><li class="menu-item menu-item-level-1"><span>Nacos </span><ul><li style="height:180px;overflow:hidden" class="menu-item menu-item-level-2"><span>What is Nacos<img style="transform:rotate(0deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/what-is-nacos.html" target="_self">What is Nacos</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/concepts.html" target="_self">Concepts</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/architecture.html" target="_self">Architecure</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/feature-list.html" target="_self">Feature list</a></li></ul></li><li style="height:324px;overflow:hidden" class="menu-item menu-item-level-2"><span>Quick Start<img style="transform:rotate(0deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/quick-start.html" target="_self">Nacos</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/quick-start-spring.html" target="_self">Nacos Spring</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/quick-start-spring-boot.html" target="_self">Nacos Spring Boot</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/quick-start-spring-cloud.html" target="_self">Nacos Spring Cloud</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/quick-start-docker.html" target="_self">Nacos Docker</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/use-nacos-with-dubbo.html" target="_self">Nacos with Dubbo</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/use-nacos-with-kubernetes.html" target="_self">Nacos with K8s</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-sync.html" target="_self">Nacos Sync</a></li></ul></li><li style="height:252px;overflow:hidden" class="menu-item menu-item-level-2"><span>User Guide<img style="transform:rotate(0deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/sdk.html" target="_self">Java SDK</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/other-language.html" target="_self">Other Language</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/open-api.html" target="_self">Open-API</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-spring.html" target="_self">Nacos Spring</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/system-configurations.html" target="_self">Nacos System Configurations</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/faq.html" target="_self">FAQ</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>Admin Guide<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/deployment.html" target="_self">Deployment Guide</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/cluster-mode-quick-start.html" target="_self">Cluster Mode Deployment</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/management-api.html" target="_self">Management OpenAPI</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/console-guide.html" target="_self">Admin Console Guide</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/monitor-guide.html" target="_self">Nacos Monitor Guide</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-config-benchmark.html" target="_self">Nacos Config Benchmark</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-naming-benchmark.html" target="_self">Nacos Naming Benchmark</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-sync-use.html" target="_self">Data Migration to Nacos</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>Contributor Guide<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/contributing.html" target="_self">Contribute to Nacos</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/contributing-flow.html" target="_self">Contributing Flow</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/pull-request.html" target="_self">Pull Request Template</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/how-to-reporting-bugs.html" target="_self">How to report bugs</a></li></ul></li><li style="height:36px;overflow:hidden" class="menu-item menu-item-level-2"><span>Community<img style="transform:rotate(-90deg)" class="menu-toggle" src="/img/arrow_down.png"/></span><ul><li class="menu-item menu-item-level-3"><a href="/en-us/docs/community.html" target="_self">Community</a></li><li class="menu-item menu-item-level-3"><a href="/en-us/docs/nacos-dev.html" target="_self">Develop Team</a></li></ul></li></ul></li></ul></div><div class="doc-content markdown-body"><h1>Nacos with Dubbo fusion become registry</h1>
<p>Nacos as Dubbo ecosystems important registry implementation, including <a href="https://github.com/dubbo/dubbo-registry-nacos"><code>dubbo-registry-nacos</code></a> is Dubbo fusion Nacos registry implementation.</p>
<h2>Preparatory work</h2>
<p>When you put <a href="https://github.com/apache/dubbo/tree/master/dubbo-registry/dubbo-registry-nacos"><code>dubbo-registry-nacos</code></a> integrated into your dubbo project before, please make sure the background nacos service has started. If you are still not familiar with the basic use of Nacos, reference <a href="https://nacos.io/en-us/docs/quick-start.html">Quick Start for Nacos</a>.</p>
<h2>Quick Start</h2>
<p>Nacos Dubbo fusion become registry procedure is very simple, general steps can be divided into &quot;increasing Maven dependency&quot; and &quot;the registry&quot;.</p>
<h3>Increasing Maven dependency</h3>
<p>First, you need to <code>dubbo-registry-nacos</code>Maven dependent on added to your project <code>pom.xml</code> file, and strongly recommend that you use the dubbo <code>2.6.5</code>:</p>
<pre><code class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">dependencies</span>&gt;</span>

    ...
        
    <span class="hljs-comment">&lt;!-- Dubbo Nacos registry dependency --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.alibaba<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>dubbo-registry-nacos<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>0.0.1<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>   
    
    <span class="hljs-comment">&lt;!-- Dubbo dependency --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.alibaba<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>dubbo<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>2.6.5<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>
    
    <span class="hljs-comment">&lt;!-- Alibaba Spring Context extension --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dependency</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">groupId</span>&gt;</span>com.alibaba.spring<span class="hljs-tag">&lt;/<span class="hljs-name">groupId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">artifactId</span>&gt;</span>spring-context-support<span class="hljs-tag">&lt;/<span class="hljs-name">artifactId</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">version</span>&gt;</span>1.0.2<span class="hljs-tag">&lt;/<span class="hljs-name">version</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">dependency</span>&gt;</span>

    ...
    
<span class="hljs-tag">&lt;/<span class="hljs-name">dependencies</span>&gt;</span>
</code></pre>
<p>When a project to add<code>dubbo-registry-nacos</code>, you don't need to explicitly logic programming to realize service discovery and registration, actual implementation is provided by the third party and then configure Naocs registry.</p>
<h3>Configuration registry</h3>
<p>Suppose you Dubbo application using the Spring Framework assembly, there will be two kinds of optional configuration method, respectively: <a href="https://mercyblitz.github.io/2018/01/18/Dubbo-%E5%A4%96%E9%83%A8%E5%8C%96%E9%85%8D%E7%BD%AE/">Dubbo Spring externalized configuration</a>, and the Spring XML configuration files, and, I strongly recommend the former.</p>
<h3>Dubbo Spring externalized configuration</h3>
<p>Dubbo Spring externalized configuration consists of Dubbo <code>2.5.8</code>  introduced new features, through the Spring <code>Environment</code> attribute automatically generate and bind the Dubbo configuration Bean, implement configuration to simplify, and lower the threshold of service development.</p>
<p>Suppose you Dubbo applications using Zookeeper as registry, and the server IP address is:<code>10.20.153.10</code> at the same time, the registered address as Dubbo externalized configuration properties are stored in <code>dubbo-config.properties</code> file, as shown below:</p>
<pre><code class="language-properties"><span class="hljs-comment">## application</span>
<span class="hljs-meta">dubbo.application.name</span> = <span class="hljs-string">your-dubbo-application</span>
<span class="hljs-comment">
## Zookeeper registry address</span>
<span class="hljs-meta">dubbo.registry.address</span> = <span class="hljs-string">zookeeper://10.20.153.10:2181</span>
<span class="hljs-attr">...</span>
</code></pre>
<p>Suppose you Nacos Server running on the Server also <code>10.20.153.10</code> , and use the default Nacos service port <code>8848</code>, you only need to <code>dubbo.registry.address</code> property adjustment is as follows:</p>
<pre><code class="language-properties"><span class="hljs-comment">## 其他属性保持不变</span>
<span class="hljs-comment">
## Nacos registry address</span>
<span class="hljs-meta">dubbo.registry.address</span> = <span class="hljs-string">nacos://10.20.153.10:8848</span>
<span class="hljs-comment">##If you want to use your own namespace, you can use the following two methods:</span>
<span class="hljs-comment">#dubbo.registry.address = nacos://10.20.153.10:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932</span>
<span class="hljs-comment">#dubbo.registry.parameters.namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932</span>
<span class="hljs-attr">...</span>
</code></pre>
<p>Then, restart your application Dubbo, Dubbo information services and consumption in Nacos console can show:</p>
<p><img src="https://img.alicdn.com/tfs/TB1n6m7zMTqK1RjSZPhXXXfOFXa-2784-1058.png" alt="image-20181213103845976-4668726.png | left | 747x284"></p>
<p>As shown, the service name prefix for <code>providers:</code> metainfo for the service provider's information, <code>consumers:</code> represents the service consumer metainfo. Click&quot;<strong>details</strong>&quot; can check the service status details:</p>
<p><img src="https://img.alicdn.com/tfs/TB1vZzfzQzoK1RjSZFlXXai4VXa-2714-1588.png" alt="image-20181213104145998-4668906.png | left | 747x437"></p>
<p>If you are using a Spring XML configuration file assembly Dubbo registry, please refer to the next section.</p>
<h3>Spring XML configuration files</h3>
<p>Similarly, suppose you Dubbo applications using Zookeeper as registry, and the server IP address is:<code>10.20.153.10</code>, and assembling Spring Bean in the XML file, as shown below:</p>
<pre><code class="language-xml"><span class="hljs-meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans"</span>
    <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">"http://www.w3.org/2001/XMLSchema-instance"</span>
    <span class="hljs-attr">xmlns:dubbo</span>=<span class="hljs-string">"http://dubbo.apache.org/schema/dubbo"</span>
    <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd"</span>&gt;</span>
 
    <span class="hljs-comment">&lt;!-- 提供方应用信息，用于计算依赖关系 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dubbo:application</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"dubbo-provider-xml-demo"</span>  /&gt;</span>
 
    <span class="hljs-comment">&lt;!-- 使用 Zookeeper 注册中心 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dubbo:registry</span> <span class="hljs-attr">address</span>=<span class="hljs-string">"zookeeper://10.20.153.10:2181"</span> /&gt;</span>
 	...
<span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span>
</code></pre>
<p>Like <a href="https://mercyblitz.github.io/2018/01/18/Dubbo-%E5%A4%96%E9%83%A8%E5%8C%96%E9%85%8D%E7%BD%AE/">Dubbo Spring externalized configuration</a> configuration, only need to adjust the <code>address</code> attribute configuration can be:</p>
<pre><code class="language-xml"><span class="hljs-meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans"</span>
    <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">"http://www.w3.org/2001/XMLSchema-instance"</span>
    <span class="hljs-attr">xmlns:dubbo</span>=<span class="hljs-string">"http://dubbo.apache.org/schema/dubbo"</span>
    <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd"</span>&gt;</span>
 
    <span class="hljs-comment">&lt;!-- 提供方应用信息，用于计算依赖关系 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dubbo:application</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"dubbo-provider-xml-demo"</span>  /&gt;</span>
 
    <span class="hljs-comment">&lt;!-- 使用 Nacos 注册中心 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dubbo:registry</span> <span class="hljs-attr">address</span>=<span class="hljs-string">"nacos://10.20.153.10:8848"</span> /&gt;</span>
     <span class="hljs-comment">&lt;!-- If you want to use your own namespace, you can use the following configuration --&gt;</span>
    <span class="hljs-comment">&lt;!-- &lt;dubbo:registry address="nacos://10.20.153.10:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932" /&gt; --&gt;</span>
 	...
<span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span>
</code></pre>
<p>After restart the Dubbo application, you can also find the provider and consumer registered metainfo is presented in Nacos console:</p>
<p><img src="https://img.alicdn.com/tfs/TB1zl2dzQPoK1RjSZKbXXX1IXXa-2784-1022.png" alt="image-20181213113049185-4671849.png | left | 747x274"></p>
<p>Whether you absolute configuration or switch Nacos registry super Easy? If you are still wanting more or less understand, may refer to the following the complete example.</p>
<h2>Complete sample</h2>
<p>Above pictures of metadata from Dubbo Spring annotations driver sample and Dubbo Spring XML configuration driven example, the following will introduce both, you can choose your preference programming model.Before the formal discussion, first to introduce the preparation work, because they are dependent on the Java service interface and <a href="http://implementation.At">implementation.At</a> the same time, <strong>please make sure that the local (<code>127.0.0.1</code>) environment has launched Nacos service</strong>.</p>
<h3>Sample interface and implementation</h3>
<p>First define the sample interface, as shown below:</p>
<pre><code class="language-java"><span class="hljs-keyword">package</span> com.alibaba.dubbo.demo.service;

<span class="hljs-comment">/**
 * DemoService
 *
 * <span class="hljs-doctag">@since</span> 2.6.5
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">interface</span> <span class="hljs-title">DemoService</span> </span>{

    <span class="hljs-function">String <span class="hljs-title">sayName</span><span class="hljs-params">(String name)</span></span>;

}
</code></pre>
<p>Provide the above interface implementation class:</p>
<pre><code class="language-java"><span class="hljs-keyword">package</span> com.alibaba.dubbo.demo.service;

<span class="hljs-keyword">import</span> com.alibaba.dubbo.config.annotation.Service;
<span class="hljs-keyword">import</span> com.alibaba.dubbo.rpc.RpcContext;

<span class="hljs-keyword">import</span> org.springframework.beans.factory.annotation.Value;

<span class="hljs-comment">/**
 * Default {<span class="hljs-doctag">@link</span> DemoService}
 *
 * <span class="hljs-doctag">@since</span> 2.6.5
 */</span>
<span class="hljs-meta">@Service</span>(version = <span class="hljs-string">"${demo.service.version}"</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DefaultService</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">DemoService</span> </span>{

    <span class="hljs-meta">@Value</span>(<span class="hljs-string">"${demo.service.name}"</span>)
    <span class="hljs-keyword">private</span> String serviceName;

    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">sayName</span><span class="hljs-params">(String name)</span> </span>{
        RpcContext rpcContext = RpcContext.getContext();
        <span class="hljs-keyword">return</span> String.format(<span class="hljs-string">"Service [name :%s , port : %d] %s(\"%s\") : Hello,%s"</span>,
                serviceName,
                rpcContext.getLocalPort(),
                rpcContext.getMethodName(),
                name,
                name);
    }
}
</code></pre>
<p>Interface and implementation after ready, the following will be driven by annotations and XML configuration driven their implementation.</p>
<h3>Spring annotations driver sample</h3>
<p>Dubbo <code>2.5.7</code> reconstructed the Spring annotations driver programming model.</p>
<h4>Provider annotation driven implementation</h4>
<ul>
<li>Definition of Dubbo provider externalized configuration properties -  <code>provider-config.properties</code></li>
</ul>
<pre><code class="language-properties"><span class="hljs-comment">## application</span>
<span class="hljs-meta">dubbo.application.name</span> = <span class="hljs-string">dubbo-provider-demo</span>
<span class="hljs-comment">
## Nacos registry address</span>
<span class="hljs-meta">dubbo.registry.address</span> = <span class="hljs-string">nacos://127.0.0.1:8848</span>
<span class="hljs-comment">##If you want to use your own namespace, you can use the following two methods:</span>
<span class="hljs-comment">#dubbo.registry.address = nacos://127.0.0.1:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932</span>
<span class="hljs-comment">#dubbo.registry.parameters.namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932</span>
<span class="hljs-comment">
## Dubbo Protocol</span>
<span class="hljs-meta">dubbo.protocol.name</span> = <span class="hljs-string">dubbo</span>
<span class="hljs-meta">dubbo.protocol.port</span> = <span class="hljs-string">-1</span>
<span class="hljs-comment">
# Provider @Service version</span>
<span class="hljs-meta">demo.service.version</span>=<span class="hljs-string">1.0.0</span>
<span class="hljs-meta">demo.service.name</span> = <span class="hljs-string">demoService</span>
</code></pre>
<ul>
<li>Implement the provider to guide class - <code>DemoServiceProviderBootstrap</code></li>
</ul>
<pre><code class="language-java"><span class="hljs-keyword">package</span> com.alibaba.dubbo.demo.provider;

<span class="hljs-keyword">import</span> com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
<span class="hljs-keyword">import</span> com.alibaba.dubbo.demo.service.DemoService;

<span class="hljs-keyword">import</span> org.springframework.context.annotation.AnnotationConfigApplicationContext;
<span class="hljs-keyword">import</span> org.springframework.context.annotation.PropertySource;

<span class="hljs-keyword">import</span> java.io.IOException;

<span class="hljs-comment">/**
 * {<span class="hljs-doctag">@link</span> DemoService} provider demo
 */</span>
<span class="hljs-meta">@EnableDubbo</span>(scanBasePackages = <span class="hljs-string">"com.alibaba.dubbo.demo.service"</span>)
<span class="hljs-meta">@PropertySource</span>(value = <span class="hljs-string">"classpath:/provider-config.properties"</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DemoServiceProviderBootstrap</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException </span>{
        AnnotationConfigApplicationContext context = <span class="hljs-keyword">new</span> AnnotationConfigApplicationContext();
        context.register(DemoServiceProviderBootstrap<span class="hljs-class">.<span class="hljs-keyword">class</span>)</span>;
        context.refresh();
        System.out.println(<span class="hljs-string">"DemoService provider is starting..."</span>);
        System.in.read();
    }
}

</code></pre>
<p>The annotation <code>@EnableDubbo</code> activation Dubbo annotation driven and externalized configuration, its <code>scanBasePackages</code> properties scanning to specify the Java package, all marked <code>@Service</code> Service interface implementation class exposure for Spring Bean, then be exported Dubbo Service.</p>
<p><code>@PropertySource</code> is Spring Framework 3.1 introduced the standard import properties annotation configuration resources, it will provide Dubbo externalized configuration.</p>
<h4>Service consumer comments drive implementation</h4>
<ul>
<li>Definition of Dubbo consumer externalized configuration properties -  <code>consumer-config.properties</code></li>
</ul>
<pre><code class="language-properties"><span class="hljs-comment">## Dubbo Application info</span>
<span class="hljs-meta">dubbo.application.name</span> = <span class="hljs-string">dubbo-consumer-demo</span>
<span class="hljs-comment">
## Nacos registry address</span>
<span class="hljs-meta">dubbo.registry.address</span> = <span class="hljs-string">nacos://127.0.0.1:8848</span>
<span class="hljs-comment">##If you want to use your own namespace, you can use the following two methods:</span>
<span class="hljs-comment">#dubbo.registry.address = nacos://127.0.0.1:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932</span>
<span class="hljs-comment">#dubbo.registry.parameters.namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932</span>
<span class="hljs-comment">
# @Reference version</span>
<span class="hljs-meta">demo.service.version</span>= <span class="hljs-string">1.0.0</span>
</code></pre>
<p>Similarly, <code>dubbo.registry.address</code> attribute points to Nacos registry, other dubbo service relevant meta information through Nacos registry access.</p>
<ul>
<li>Implementation services consumer guide - <code>DemoServiceConsumerBootstrap</code></li>
</ul>
<pre><code class="language-java"><span class="hljs-keyword">package</span> com.alibaba.dubbo.demo.consumer;

<span class="hljs-keyword">import</span> com.alibaba.dubbo.config.annotation.Reference;
<span class="hljs-keyword">import</span> com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
<span class="hljs-keyword">import</span> com.alibaba.dubbo.demo.service.DemoService;

<span class="hljs-keyword">import</span> org.springframework.context.annotation.AnnotationConfigApplicationContext;
<span class="hljs-keyword">import</span> org.springframework.context.annotation.PropertySource;

<span class="hljs-keyword">import</span> javax.annotation.PostConstruct;
<span class="hljs-keyword">import</span> java.io.IOException;

<span class="hljs-comment">/**
 * {<span class="hljs-doctag">@link</span> DemoService} consumer demo
 */</span>
<span class="hljs-meta">@EnableDubbo</span>
<span class="hljs-meta">@PropertySource</span>(value = <span class="hljs-string">"classpath:/consumer-config.properties"</span>)
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DemoServiceConsumerBootstrap</span> </span>{

    <span class="hljs-meta">@Reference</span>(version = <span class="hljs-string">"${demo.service.version}"</span>)
    <span class="hljs-keyword">private</span> DemoService demoService;

    <span class="hljs-meta">@PostConstruct</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
            System.out.println(demoService.sayName(<span class="hljs-string">"小马哥（mercyblitz）"</span>));
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException </span>{
        AnnotationConfigApplicationContext context = <span class="hljs-keyword">new</span> AnnotationConfigApplicationContext();
        context.register(DemoServiceConsumerBootstrap<span class="hljs-class">.<span class="hljs-keyword">class</span>)</span>;
        context.refresh();
        context.close();
    }
}

</code></pre>
<p>Similarly, <code>@EnableDubbo</code> annotations to activate Dubbo annotation driven and externalized configuration, but the current belong to the Service consumers, without having to specify the Java package name scan label <code>@Service</code> Service implementation.</p>
<p><code>@Reference</code> is Dubbo remote service dependency injection annotations, need service providers and consumers agreed interface (interface), version (version) and group (group) information.Example, in the current service consumption <code>DemoService</code> service version from the configuration properties file <code>consumer-config.properties</code>.</p>
<p><code>@PostConstruct</code> code shows when <code>DemoServiceConsumerBootstrap</code> Bean initialization, execution Dubbo ten times remote method invocation.</p>
<h4>Run annotation driver sample</h4>
<p>Twice in the local boot <code>DemoServiceProviderBootstrap</code>, the registry will appear two health services:</p>
<p><img src="https://img.alicdn.com/tfs/TB1s9fbzMHqK1RjSZFgXXa7JXXa-2390-122.png" alt="image-20181213123909636-4675949.png | left | 747x38"></p>
<p>Run again <code>DemoServiceConsumerBootstrap</code>, run results as follows:</p>
<pre><code>Service [name :demoService , port : 20880] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :demoService , port : 20881] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :demoService , port : 20880] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :demoService , port : 20880] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :demoService , port : 20881] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :demoService , port : 20881] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :demoService , port : 20880] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :demoService , port : 20880] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :demoService , port : 20881] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :demoService , port : 20881] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
</code></pre>
<p>Operate, and service consumer using the load balancing strategy, RPC calls ten times the average contribution to two Dubbo provider instance.</p>
<h3>Spring XML configuration driver sample</h3>
<p>The Spring XML configuration driven programming model is a traditional Spring assembly components.</p>
<h4>Provider XML configuration</h4>
<ul>
<li>Define the service provider XML context configuration file - <code>/META-INF/spring/dubbo-provider-context.xml</code></li>
</ul>
<pre><code class="language-xml"><span class="hljs-meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans"</span>
       <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">"http://www.w3.org/2001/XMLSchema-instance"</span>
       <span class="hljs-attr">xmlns:dubbo</span>=<span class="hljs-string">"http://dubbo.apache.org/schema/dubbo"</span>
       <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd"</span>&gt;</span>

    <span class="hljs-comment">&lt;!-- 提供方应用信息，用于计算依赖关系 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dubbo:application</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"dubbo-provider-xml-demo"</span>/&gt;</span>

    <span class="hljs-comment">&lt;!-- 使用 Nacos 注册中心 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dubbo:registry</span> <span class="hljs-attr">address</span>=<span class="hljs-string">"nacos://127.0.0.1:8848"</span>/&gt;</span>
    <span class="hljs-comment">&lt;!-- If you want to use your own namespace, you can use the following configuration --&gt;</span>
    <span class="hljs-comment">&lt;!-- &lt;dubbo:registry address="nacos://127.0.0.1:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932" /&gt; --&gt;</span>

    <span class="hljs-comment">&lt;!-- 用dubbo协议在随机端口暴露服务 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dubbo:protocol</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"dubbo"</span> <span class="hljs-attr">port</span>=<span class="hljs-string">"-1"</span>/&gt;</span>

    <span class="hljs-comment">&lt;!-- 声明需要暴露的服务接口 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dubbo:service</span> <span class="hljs-attr">interface</span>=<span class="hljs-string">"com.alibaba.dubbo.demo.service.DemoService"</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">"demoService"</span> <span class="hljs-attr">version</span>=<span class="hljs-string">"2.0.0"</span>/&gt;</span>

    <span class="hljs-comment">&lt;!-- 和本地bean一样实现服务 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">bean</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"demoService"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"com.alibaba.dubbo.demo.service.DefaultService"</span>/&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span>
</code></pre>
<ul>
<li>Implement the provider to guide class - <code>DemoServiceProviderXmlBootstrap</code></li>
</ul>
<pre><code class="language-xml">package com.alibaba.dubbo.demo.provider;

import com.alibaba.dubbo.demo.service.DemoService;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

/**
 * {@link DemoService} provider demo XML bootstrap
 */
public class DemoServiceProviderXmlBootstrap {

    public static void main(String[] args) throws IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
        context.setConfigLocation("/META-INF/spring/dubbo-provider-context.xml");
        context.refresh();
        System.out.println("DemoService provider (XML) is starting...");
        System.in.read();
    }
}
</code></pre>
<h4>Service consumer driven XML configuration</h4>
<ul>
<li>Define the service consumer context XML configuration files - <code>/META-INF/spring/dubbo-consumer-context.xml</code></li>
</ul>
<pre><code class="language-xml"><span class="hljs-meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">beans</span> <span class="hljs-attr">xmlns</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans"</span>
       <span class="hljs-attr">xmlns:xsi</span>=<span class="hljs-string">"http://www.w3.org/2001/XMLSchema-instance"</span>
       <span class="hljs-attr">xmlns:dubbo</span>=<span class="hljs-string">"http://dubbo.apache.org/schema/dubbo"</span>
       <span class="hljs-attr">xsi:schemaLocation</span>=<span class="hljs-string">"http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd        http://dubbo.apache.org/schema/dubbo        http://dubbo.apache.org/schema/dubbo/dubbo.xsd"</span>&gt;</span>

    <span class="hljs-comment">&lt;!-- 提供方应用信息，用于计算依赖关系 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dubbo:application</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"dubbo-consumer-xml-demo"</span>/&gt;</span>

    <span class="hljs-comment">&lt;!-- 使用 Nacos 注册中心 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dubbo:registry</span> <span class="hljs-attr">address</span>=<span class="hljs-string">"nacos://127.0.0.1:8848"</span>/&gt;</span>
    <span class="hljs-comment">&lt;!-- If you want to use your own namespace, you can use the following configuration --&gt;</span>
    <span class="hljs-comment">&lt;!-- &lt;dubbo:registry address="nacos://127.0.0.1:8848?namespace=5cbb70a5-xxx-xxx-xxx-d43479ae0932" /&gt; --&gt;</span>

    <span class="hljs-comment">&lt;!-- 引用服务接口 --&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">dubbo:reference</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"demoService"</span> <span class="hljs-attr">interface</span>=<span class="hljs-string">"com.alibaba.dubbo.demo.service.DemoService"</span> <span class="hljs-attr">version</span>=<span class="hljs-string">"2.0.0"</span>/&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">beans</span>&gt;</span>
</code></pre>
<ul>
<li>Implementation services consumer guide - <code>DemoServiceConsumerXmlBootstrap</code></li>
</ul>
<pre><code class="language-java"><span class="hljs-keyword">package</span> com.alibaba.dubbo.demo.consumer;

<span class="hljs-keyword">import</span> com.alibaba.dubbo.demo.service.DemoService;

<span class="hljs-keyword">import</span> org.springframework.context.support.ClassPathXmlApplicationContext;

<span class="hljs-keyword">import</span> java.io.IOException;

<span class="hljs-comment">/**
 * {<span class="hljs-doctag">@link</span> DemoService} consumer demo XML bootstrap
 */</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DemoServiceConsumerXmlBootstrap</span> </span>{

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> <span class="hljs-keyword">throws</span> IOException </span>{
        ClassPathXmlApplicationContext context = <span class="hljs-keyword">new</span> ClassPathXmlApplicationContext();
        context.setConfigLocation(<span class="hljs-string">"/META-INF/spring/dubbo-consumer-context.xml"</span>);
        context.refresh();
        System.out.println(<span class="hljs-string">"DemoService consumer (XML) is starting..."</span>);
        DemoService demoService = context.getBean(<span class="hljs-string">"demoService"</span>, DemoService<span class="hljs-class">.<span class="hljs-keyword">class</span>)</span>;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++) {
            System.out.println(demoService.sayName(<span class="hljs-string">"小马哥（mercyblitz）"</span>));
        }
        context.close();
    }
}
</code></pre>
<h4>Run the XML configuration driven example</h4>
<p>Similarly, to start the first two <code>DemoServiceProviderXmlBootstrap</code> bootstrap class, observe Nacos registry service provider changes:</p>
<p><img src="https://img.alicdn.com/tfs/TB1HCfbzMHqK1RjSZFgXXa7JXXa-2388-106.png" alt="image-20181213125527201-4676927.png | left | 747x33"></p>
<p>XML configuration driven service version for <code>2.0.0</code>, therefore the registration service and correct.</p>
<p>Again run service consumers leading class <code>DemoServiceConsumerXmlBootstrap</code>, watch the console output:</p>
<pre><code>Service [name :null , port : 20882] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :null , port : 20882] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :null , port : 20883] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :null , port : 20882] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :null , port : 20882] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :null , port : 20883] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :null , port : 20882] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :null , port : 20883] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :null , port : 20883] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
Service [name :null , port : 20883] sayName(&quot;小马哥（mercyblitz）&quot;) : Hello,小马哥（mercyblitz）
</code></pre>
<p>Results also runs and load balancing is normal, but because the current sample has yet to add attributes <code>demo.service.name</code> of therefore, &quot;name&quot; part of the information output <code>null</code>.</p>
<p>If your attention or love Dubbo and Nacos open source project, as well as for their points of &quot;star&quot;, related links:</p>
<ul>
<li>Apache Dubbo：<a href="https://github.com/apache/dubbo">https://github.com/apache/dubbo</a></li>
<li>Dubbo Nacos Registry：<a href="https://github.com/apache/dubbo/tree/master/dubbo-registry/dubbo-registry-nacos">https://github.com/apache/dubbo/tree/master/dubbo-registry/dubbo-registry-nacos</a></li>
<li>Alibaba Nacos：<a href="https://github.com/alibaba/nacos">https://github.com/alibaba/nacos</a></li>
</ul>
</div></div></section><footer class="footer-container"><div class="footer-body"><img src="/img/nacos_gray.png"/><div class="cols-container"><div class="col col-12"><h3>Vision</h3><p>By providing an easy-to-use service infrastructure such as dynamic service discovery, service configuration, service sharing and management and etc., Nacos help users better construct, deliver and manage their own service platform, reuse and composite business service faster and deliver value of business innovation more quickly so as to win market for users in the era of cloud native and in all cloud environments, such as private, mixed, or public clouds.</p></div><div class="col col-6"><dl><dt>Documentation</dt><dd><a href="/en-us/docs/what-is-nacos.html" target="_self">Overview</a></dd><dd><a href="/en-us/docs/quick-start.html" target="_self">Quick start</a></dd><dd><a href="/en-us/docs/contributing.html" target="_self">Developer guide</a></dd></dl></div><div class="col col-6"><dl><dt>Resources</dt><dd><a href="/en-us/community/index.html" target="_self">Community</a></dd><dd><a href="https://www.aliyun.com/product/acm?source_type=nacos_pc_20181219" target="_self">Cloud Service ACM</a></dd><dd><a href="https://www.aliyun.com/product/edas?source_type=nacos_pc_20181219" target="_self">Cloud Service EDAS</a></dd><dd><a href="https://www.aliyun.com/product/ahas?source_type=nacos_pc_20190225" target="_self">Cloud Service AHAS</a></dd></dl></div></div><div class="copyright"><span>@ 2018 The Nacos Authors | An Alibaba Middleware (Aliware) Project</span></div></div></footer></div></div>
	<script src="https://f.alicdn.com/react/15.4.1/react-with-addons.min.js"></script>
	<script src="https://f.alicdn.com/react/15.4.1/react-dom.min.js"></script>
	<script>
		window.rootPath = '';
  </script>
	<script src="/build/documentation.js"></script>
</body>
</html>